<template>
  <el-dialog title="提示" :visible.sync="show" width="600px">
    <div>
      <el-form ref="ruleForm" :rules="rules" label-position="top" label-width="80px" :model="ruleForm">
        <el-form-item label="评分" prop="score" required>
          <el-rate v-model="ruleForm.score" />
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="ruleForm.desc" type="textarea" />
        </el-form-item>
      </el-form>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button @click="show = false">取 消</el-button>
      <el-button type="primary" @click="onAdd">确 定</el-button>
    </div>
  </el-dialog>
</template>
<script>
import { mapGetters } from 'vuex'
import { http } from '@/api'

export default {
  name: 'PageAdd',
  data() {
    return {
      show: false,
      ruleForm: {

      },
      rules: {},
      currentItem: {},
      studentList: [],
      courseList: []
    }
  },
  created() {
  },
  computed: {
    ...mapGetters(['categoryList', 'userInfo'])
  },
  methods: {
    open(item) {
      this.ruleForm = {
        category_id: item.category_id,
        course_id: item.course_id,
        teacher_id: item.teacher_id,
        student_id: item.student_id,
        score: '',
        desc: '',
        course_student_id: item.id
      }
      this.show = true
    },
    onAdd() {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          this.$emit('save', {
            ...this.ruleForm
          })
        }
      })
    },
    changeCategory(val) {
      this.ruleForm.course_id = ''
      http.course.list({
        category_id: val
      }).then(res => {
        console.log(res)
        this.courseList = res
      })
      this.changeCourse('')
    },
    changeCourse(val) {
      this.ruleForm.student_id = ''
      if (val) {
        http.course_student.list({
          course_id: val
        }).then(res => {
          if (!res.length) {
            this.studentList = []
            this.ruleForm.student_id = ''
            return
          }
          this.getStudentList(res.map(el => el.student_id).join(','))
        })
      }
    },
    getStudentList(ids) {
      http.student.list({
        ids
      }).then(res => {
        console.log(res)
        this.studentList = res
      })
    }
  }
}
</script>

<style scoped>

</style>
